/** @format */

const { Controller } = require("egg");

class PostController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = "hi, egg";
    }
    async addpostings() {
        const { ctx } = this;
        let clientdata = ctx.request.body;

        // let results = await this.app.mysql.select("thumbsup", {
        //     // 搜索 posts 表
        //     where: { post_id: "draft", user_id: userinfo.userid }, // WHERE 条件
        //     columns: ["author", "title"], // 要查询的字段
        //     orders: [
        //         ["created_at", "desc"],
        //         ["id", "desc"],
        //     ], // 排序方式
        //     limit: 10, // 返回数据量
        //     offset: 0, // 数据偏移量
        // });
        let post = await this.app.mysql.insert("postings", {
            post_title: clientdata.post_title,
            author_id: clientdata.author_id,
            post_detail: clientdata.post_detail,
        });

        if (post) {
            ctx.body = {
                code: 200,
                desp: "添加成功",
                post,
            };
        } else {
            ctx.body = {
                code: 201,
                desp: "添加失败",
            };
        }
    }
    async getpostings() {
        const { ctx, app } = this;
        let userinfo = {};
        let clientdata = ctx.request.body;
        try {
            userinfo = app.jwt.verify(ctx.request.header.token, app.config.jwt.secret);
            console.log(userinfo, "我是用户");

            // console.log(post, 'post')
        } catch (error) {
            console.error(error);
            userinfo = false;
            // Expected output: ReferenceError: nonExistentFunction is not defined
            // (Note: the exact output may be browser-dependent)
        }
        let data = {
            // 搜索 posts 表
            where: { ...(clientdata.Query ? clientdata.Query : "") }, // WHERE 条件
            limit: clientdata.pagesize, // 返回数据量
            offset: (clientdata.pageindex - 1) * clientdata.pagesize, // 数据偏移量
            orders: [["id"]], // 排序方式
        };
        console.log(data, "data");
        let postingslist = await this.app.mysql.select("postings", data);
        await Promise.all(
            postingslist.map(async (item, index) => {
                let authorinfo = await this.app.mysql.get("userinfo", { id: item.author_id });
                postingslist[index].authorinfo = authorinfo;
				let commentslist = await this.app.mysql.select("comments",{ where: { posting_id: item.id }});
				postingslist[index].commentslist = commentslist;
                let results = await this.app.mysql.select("thumbsup", {
                    where: { post_id: item.id, user_id: userinfo.userid }, // WHERE 条件
                });
                console.log(results, "results");
                if (results.length) {
                    postingslist[index].belike = results[0];
                }
            }),
        );
        if (postingslist) {
            ctx.body = {
                code: 200,
                desp: "查询成功",
                postingslist,
            };
        }
    }
    async addposlike() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        const post = await this.app.mysql.insert("thumbsup", {
            user_id: clientdata.user_id,
            post_id: clientdata.post_id,
        });
        if (post) {
            ctx.body = {
                code: 200,
                desp: "点赞成功",
                post,
            };
        } else {
            ctx.body = {
                code: 201,
                desp: "添加失败",
            };
        }
    }
    async deleteposlike() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        const result = await this.app.mysql.delete("thumbsup", {
            id: clientdata.id,
        });
        if (result) {
            ctx.body = {
                code: 200,
                desp: "取消点赞",
            };
        } else {
            ctx.body = {
                code: 201,
                desp: "操作失败",
            };
        }
    }async addcomments() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        const post = await this.app.mysql.insert("comments", {
            posting_id: clientdata.posting_id,
			publication_id: clientdata.publication_id,  
			ansercommit_id: clientdata.ansercommit_id,
            content: clientdata.content,
			publication_name:clientdata.publication_name,
			anser_name:clientdata.anser_name
        });
        if (post) {
            ctx.body = {
                code: 200,
                desp: "评论成功",
                post,
            };
        } else {
            ctx.body = {
                code: 201,
                desp: "评论失败",
            };
        }
    }
}


module.exports = PostController;
